//
//  YYEVisitorView.swift
//  weibo14
//
//  Created by mac on 16/3/3.
//  Copyright © 2016年 mac. All rights reserved.
//  登录界面的设置

import UIKit
import SnapKit

/// 协议
// 在Swift要使用weak修饰的代理,那么其遵守的协议需要继承于基协议 NSObjectProtocol
// 协议的可选方法的两个关键字: @objc & optional

@objc protocol YYEVisitorViewDelegate:NSObjectProtocol {

    // 可选协议
    optional func visitorView(visitorView: YYEVisitorView, loginButtonDidCliked: UIButton);
}


class YYEVisitorView: UIView {

    
    // 代理
    weak var delegate:YYEVisitorViewDelegate?
    
   
    // 重写他的initWithFrame 方法  在里面构造

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 搭建UI
        setupUI()
        // 开启转圈动画
        startAnimation()
    }
    
    private func startAnimation() {
    
        // 初始化动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        // 指导动画结束
        anim.toValue = M_PI * 2
        
        // 重复的次数
        anim.repeatCount = MAXFLOAT

        // 时间
        anim.duration = 20
        
        // 切换后台不让动画停止
        anim.removedOnCompletion = false
        
        circleView.layer.addAnimation(anim, forKey: "id")
    }
    
    // MARK: - 提供一个方法,供外界使用,改变图片和消息框
    ///
    /// - parameter imageNamed: 图片的名字 可以为空
    /// - parameter message:    消息文字 也可为空
    func setPageInfo(imageNamed: String?, message: String?){
    
    
        if imageNamed == nil {
            
            startAnimation()
        } else {
        
            iconView.image = UIImage(named: imageNamed!)
            circleView.hidden = true
            messageLabel.text = message!
        }
        
    
    }
    
    
    // MARK: - 懒加载控件
    
    // 大图标
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    // 消息lable
    private lazy var messageLabel:UILabel = {
    
        let lable = UILabel(textColor: UIColor.darkGrayColor(), fontSzie: 14)
        
        lable.numberOfLines = 0
        
        // 居中
        lable.textAlignment = .Center
        
        lable.text = "关注一些人，回这里看看有什么惊喜"
        
        return lable
    }()
    
    private lazy var registerButton:UIButton = {
        
        let button = UIButton(textColor: UIColor.orangeColor(), fontSize: 14)
        
        button.addTarget(self, action: "loginClick:", forControlEvents: .TouchUpInside)
        
        button.setTitle("注册", forState: .Normal)
        
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: .Normal)
        
        return button
    }()
    
    private lazy var loginButton:UIButton = {
        
        let button = UIButton(textColor: UIColor.orangeColor(), fontSize: 14)
        
        button.addTarget(self, action: "loginClick:", forControlEvents: .TouchUpInside)
        
        button.setTitle("登陆", forState: .Normal)
        
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: .Normal)
        
        return button
    }()
    
    // 圆圈
     private lazy var circleView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    // 遮罩层
    private lazy var maskIconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    // 监听按钮的方法
    @objc private func loginClick(button:UIButton) {
    
    
        delegate?.visitorView?(self, loginButtonDidCliked: button)
    
    }
    
    // required 为必须现实的方法 意思是不能从文件xib中进行加载
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    
}
extension YYEVisitorView {
    // 代码太多 放在这个分类当中
    private func setupUI() {
        
        // 背景颜色
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        //        RGB(r: <#T##CGFloat#>, g: <#T##CGFloat#>, b: <#T##CGFloat#>) 返回的UIColor
        
        // 添加控件 图片添加的顺序是后来居上的
        addSubview(circleView)
        addSubview(maskIconView)
      
        
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        

        
        
        // 添加约束

        iconView.snp_makeConstraints { (make) -> Void in
        
            make.center.equalTo(self)
        }

        
        circleView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(iconView)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconView)
            make.top.equalTo(circleView.snp_bottom)
            make.width.equalTo(224)
            
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(messageLabel)
            
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            
            make.width.equalTo(100)
            
        }

        loginButton.snp_makeConstraints { (make) -> Void in
            
            make.trailing.equalTo(messageLabel)
            
            make.top.equalTo(registerButton)
            
            make.width.equalTo(100)
            
        }
        

        
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView)
        }
    

    }
    
    
    
    // 代码太多 放在这个分类当中
    private func setupUI2() {
        
        // 背景颜色
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
//        RGB(r: <#T##CGFloat#>, g: <#T##CGFloat#>, b: <#T##CGFloat#>) 返回的UIColor
        
        // 添加控件 图片添加的顺序是后来居上的
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 添加约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
        circleView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
    
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: .Bottom, multiplier: 1, constant: 16))
    
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 224))
        
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: .Leading, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: .Trailing, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
    }
    

}

